$(function () {
    var qyjgId = $("#qyjgFkfsForm [name='qyjg.id']").val();

    if (qyjgId) {
        $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm': 'fkfs'}, function (data) {
            qyjg_fkfs_datagrid(data, qyjgId)
        })
    } else {
        $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm': 'fkfs'}, function (data) {
            qyjg_fkfs_datagrid(data, -1)
        })
    }


    $("#qyjg_fkfs_tj").click(function () {
        $("#qyjg_fkfs_table").edatagrid("saveRow");
        var data = $("#qyjg_fkfs_table").datagrid("getRows");
        var row = data[data.length - 1];
        if (row && row.fksj) {
            qyjg_newDataRow(parseFloat(row.fksj) + 1, "");
        } else {
            qyjg_newDataRow(0, "");
        }
    });
    $("#qyjg_fkfs_sc").click(function () {
        var row = $("#qyjg_fkfs_table").edatagrid("getSelected");
        if (row) {
            var index = $("#qyjg_fkfs_table").edatagrid("getRowIndex", row);
            $("#qyjg_fkfs_table").edatagrid("deleteRow", index);
        }
    });

    //模板加载
    $("#qyjg_fkfs_mb").click(function () {
        var qyjgId = $("[name='qyjg.id']").val();
        $("#qyjg_fkfs_table").edatagrid("loadData", {
            total: 1,
            rows: [
                {
                    'fklx.id': 8,
                    'fksjStart': 0,
                    'fksj': 60,
                    'fkgs': '0.6%',
                    'qyjg.id': qyjgId
                },
                {
                    'fklx.id': 8,
                    'fksjStart': 61,
                    'fksj': 90,
                    'fkgs': '1.1%',
                    'qyjg.id': qyjgId
                },
                {
                    'fklx.id': 9,
                    'fksjStart': 0,
                    'fksj': 60,
                    'fkgs': '1.0%',
                    'qyjg.id': qyjgId
                },
                {
                    'fklx.id': 9,
                    'fksjStart': 61,
                    'fksj': 90,
                    'fkgs': '1.5%',
                    'qyjg.id': qyjgId
                }
            ]
        })
    });
});


function qyjg_newDataRow(fksjStart, fksj) {
    $('#qyjg_fkfs_table').datagrid('appendRow', {
        fklx_z: '',
        fksjStart: fksjStart,
        fksj: fksj,
        fkgs: ''
    });


}


function qyjg_fkfs_datagrid(fsOptions, qyjgId) {

    $("#qyjg_fkfs_table").edatagrid({
        fit: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        url: $.webPath() + "/organ/qyjg/qyjgfkfsList?qyjgId=" + qyjgId,
        columns: [
            [
                {field: 'fklx.id', editor: {type: 'combobox', options: {
                    data: fsOptions,
                    valueField: 'id',
                    textField: 'z',
                    multiple: false,
                    panelHeight: '55'
                }
                }, formatter: function (value) {
                    for (var i = 0; i < fsOptions.length; i++) {
                        if (fsOptions[i].id == value) return fsOptions[i].z;
                    }
                    return value;
                }, title: '付款类型', width: 50},
                {field: 'fksjStart', title: '周期开始时间(天)', width: 100, editor: {type: 'numberbox', options: { min: 0}}},
                {field: 'fksj', title: '周期结束时间(天)', width: 100, align: 'right', editor: {type: 'numberbox', options: { min: 0}}},
                {field: 'fkgs', title: '利率公式(A作为占用天数)', width: 100, align: 'right', editor: {type: 'text'}}
            ]
        ]
    });

}


